# delimit ;
clear ;
set more off ;
est drop _all ;
cd "R:/personlig/fenellac/menarche_replication/build" ;

* **************************************************************************** ;
* This code constructs a dataset on eligible women from IHDS 2 (2011-2012) 
* containing the outcome variables. Eligible women are ever-married 15-49 
*
* Paper: "Age of Marriage and Women's Political Engagement: Evidence from India"
* Authors: Fenella Carpena, Francesca Jensenius
* 
* Code by Fenella Carpena
* Last update: April 28, 2020
* **************************************************************************** ;

****************************************************************************** ;
* get datasets that I will need 
****************************************************************************** ;

* eligible women data ;
use "./temp/ihds02-DS03-eligible-women.dta", clear ;
isid IDHH PERSONID ;

save "./temp/temp-women", replace ;

* household roster data ;
use "./temp/ihds02-DS01-individual.dta", clear ;
	
	* one person has a missing personID, so drop this observation ;
	drop if missing(PERSONID) & missing(IDPERSON) ;
	
isid IDHH PERSONID ;
save "./temp/temp-hh-roster", replace ;
	
* household level data ;
use "./temp/ihds02-DS02-household.dta", clear ;
isid IDHH ;
save "./temp/temp-hh", replace ;

* wage and salary (listing of individuals with data) ;
use "./temp/ihds02-DS10-wage-salary-hh-roster", clear ;
drop WS* ;
duplicates drop ;
isid STATEID DISTID PSUID HHID HHSPLITID PERSONID ;
save "./temp/temp-wage-salary-listing", replace ;

****************************************************************************** ;
* merge all datasets together ;
****************************************************************************** ;

* use eligible women data as master ;
use "./temp/temp-women", clear ;

* merge with HH roster data ;
merge 1:1 IDHH PERSONID using "./temp/temp-hh-roster" ;
tab _merge ;
assert _merge != 1 ;
keep if _merge == 3 ;
drop _merge ;

* merge with HH level data ;
merge m:1 IDHH using "./temp/temp-hh" ; 
tab _merge ;
assert _merge != 1 ;
keep if _merge == 3 ;
drop _merge ;

* merge with listing of persons in the wage-salary data ;
merge 1:1 STATEID DISTID PSUID HHID HHSPLITID PERSONID using "./temp/temp-wage-salary-listing" ;
tab _merge ;

	* respondent worked for wage iff they were in the wages-salary data ;
	gen has_wage_salary_data = _merge == 3 ; 
	drop if _merge == 2 ;
	drop _merge ;

****************************************************************************** ;
* LHS variables from HH survey: Wage and salary work ;
****************************************************************************** ;

gen wagework_any = has_wage_salary_data ;
drop has_wage_salary_data ;
label var wagework_any "Worked for wages/salary last year" ;

****************************************************************************** ;
* LHS variables from HH survey: Education and literacy ;
****************************************************************************** ;

gen can_read_write = ED2 ;
label var can_read_write "Can read and write a sentence" ;

gen can_converse_english = ED3 == 1 | ED3 == 2 if ED3 != . ;
label var can_converse_english "Can converse in English" ;

gen ever_schooled = ED4 ;
label var ever_schooled "Ever attended school" ;

gen years_schooling = ED6 ;
label values years_schooling ED6 ;
label var years_schooling "Years of schooling completed" ;

	* check that years_schooling == 0 if never attended school ;
	assert years_schooling == 0 if ever_schooled == 0 ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Gender relations module, Decision-making
****************************************************************************** ;

* Respondent has the most say in the decision ;
gen majorsay_cook        = GR1G == 1 if GR1G != . ;
gen majorsay_purchase    = GR2G == 1 if GR2G != . ;
gen majorsay_children    = GR3G == 1 if GR3G != . ;
gen majorsay_sick		 = GR4G == 1 if GR4G != . ; * Note: this question in Wave 2 only ;
gen majorsay_buy_land    = GR5G == 1 if GR5G != . ; * Note: this question in Wave 2 only ;
gen majorsay_social 	 = GR6G == 1 if GR6G != . ; * Note: this question in Wave 2 only ;

label var majorsay_cook "Has major say: what to cook on a daily basis" ;
label var majorsay_purchase "Has major say: whether to buy expensive item" ;
label var majorsay_children "Has major say: how many children you have" ;
label var majorsay_sick "Has major say: what to do if you fall sick" ;
label var majorsay_buy_land "Has major say: buy land or property" ;
label var majorsay_social "Has major say: how much to spend on social functions" ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Gender relations module, Mobility
****************************************************************************** ;

* Can go alone to [...] ;
gen alone_healthcenter 	= GR9F == 1  if GR9F  != . ; 
gen alone_friendshome  	= GR10F == 1 if GR10F != . ; 
gen alone_kiranashop   	= GR11F == 1 if GR11F != . ; 
gen alone_trainbus 		= GR12F == 1 if GR12F != . ; * Note: this question in Wave 2 only ;

label var alone_healthcenter "Can go alone: local health center" ;
label var alone_friendshome "Can go alone: relatives/friends in the village" ;
label var alone_kiranashop "Can go alone: kirana shop" ;
label var alone_trainbus "Can go alone: short distance by train/bus" ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Gender relations module, Pol participation
****************************************************************************** ;

* Participation in political org ;
gen member_pol_org = GR18D == 1 if GR18D != . ; 
label var member_pol_org "Eligible woman is a member of a political org" ;

* Attended panchayat meeting last year ;
gen attended_panchayat = GR19 == 1 if GR19 != . ;
label var attended_panchayat "Eligible woman attended public mtg by panchayat/nagarpalika/ward comm last year" ;

* Discusses politics with husband ;
gen discuss_politics    = GR29C == 1 | GR29C == 2 if GR29C != . ;
label var discuss_politics "Discusses community/politics w/ husband" ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Gender relations module, Work
****************************************************************************** ;

* Have you ever worked for pay or wages ; 
gen ever_work = GR46 == 1 if GR46 != . ;
label var ever_work "Ever worked for pay" ;

* Currently working for pay or wages ;
gen current_work = GR46B == 1 if GR46B != . ;
replace current_work = 0 if GR46 == 0 ;
label var current_work "Currently working for pay" ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Eligible woman's profile ;
****************************************************************************** ;

gen num_children_alive = EW9 ;
replace num_children_alive = 0 if FH5CK == 0 & num_children_alive == . ; * Note: FH5CK is the number of total children incl dead, etc. ;
replace num_children_alive = FH2S + FH3S + FH2D + FH3D if num_children_alive == . ; * Note: FH2S, FH3S, FH2D, FH3D is the number of children living at/away from home ;
label var num_children_alive "No. of children alive" ;

****************************************************************************** ;
* LHS variables from Educ/Health survey: Fertility History
****************************************************************************** ;

* Total number of children born ;
gen num_children_total = FH5CK ;
label var num_children_total "No. children total" ; 

* Number of children who live with the respondent ;
gen num_children_with_resp = FH2CK ;
label var num_children_with_resp "No. children living with respondent" ;

****************************************************************************** ;
* save data ;
****************************************************************************** ;

* keep track of the survey wave ;
gen ihds_wave = 2 ;
label var ihds_wave "IHDS Wave" ;

* delete all other variables that are not needed ;
drop SURVEY ;
drop GE10A-HI11 ;
drop METRO ; 
drop NEWQ-DEFLATOR ;
drop ID11-NEWQELIGIBLE ;
drop RO7-MGMONTHS1 ;
drop HS3D-WS12HH ;
drop WTEW-INDFWT ;

tostring STATEID DISTID PSUID HHID IDHH HHSPLITID PERSONID, replace ;

* organize variable list ;
order STATEID-DISTRICT ihds_wave
attended_panchayat discuss_politics member_pol_org 
ever_schooled years_schooling can_read_write can_converse_english
num_children_with_resp num_children_alive num_children_tot
ever_work wagework_any current_work
alone_healthcenter alone_friendshome alone_kiranashop alone_trainbus 
majorsay_cook majorsay_purchase majorsay_children majorsay_sick majorsay_buy_land majorsay_social ;

saveold "./output/eligible-women-ihds02-lhs.dta", replace version(12) ;

erase "./temp/temp-women.dta" ;
erase "./temp/temp-hh-roster.dta" ;
erase "./temp/temp-hh.dta" ;
erase "./temp/temp-wage-salary-listing.dta" ;

exit ;
